package edu.ycu.aladdin.collector.service;

import com.alibaba.dubbo.config.annotation.Service;
import edu.ycu.aladdin.collector.api.WeiboUserInfoService;
import edu.ycu.aladdin.collector.api.entity.weibo.UserInfo;
import edu.ycu.aladdin.collector.utils.HttpUtils;
import edu.ycu.aladdin.collector.utils.ParseUtils;
import edu.ycu.aladdin.collector.weibo.RequestParam;
import edu.ycu.aladdin.common.constant.MongoConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * 微博用户信息服务 mongo 实现
 */
@Slf4j
@Component
@Service(interfaceClass = WeiboUserInfoService.class, timeout = 10000)
public class WeiboUserInfoMongoService implements WeiboUserInfoService {

    private final MongoTemplate mongoTemplate;

    @Value("${weibo.template.user-info-url}")
    private String infoUrl;

    public WeiboUserInfoMongoService(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public UserInfo addUser(Long uid, boolean cover) throws IOException {

        String json = HttpUtils.get(infoUrl.replace("{{uid}}", uid.toString()));
        log.info("request url:{}, response data:{}", infoUrl, json);
        UserInfo userInfo = ParseUtils.parseUserInfoFromJson(json);
        return addUser(userInfo, cover);
    }

    @Override
    public UserInfo addUser(UserInfo userInfo, boolean cover) {
        Query query = new Query(Criteria.where("_id").is(userInfo.getId()));
        UserInfo dbInfo = getUserInfo(userInfo.getId());
        if (dbInfo==null) {
            userInfo.setCreateTime(System.currentTimeMillis());
            mongoTemplate.insert(userInfo, MongoConstant.WEIBO_USER_COLLECTION);
        } else {
            if (cover) {
                userInfo.setCreateTime(dbInfo.getCreateTime());
                userInfo.setUpdateTime(System.currentTimeMillis());
                mongoTemplate.remove(query, MongoConstant.WEIBO_USER_COLLECTION);
                mongoTemplate.insert(userInfo, MongoConstant.WEIBO_USER_COLLECTION);
            }
        }
        return userInfo;
    }

    @Override
    public long delUser(Long uid) {
        Query query = new Query(Criteria.where("_id").is(uid));
        return mongoTemplate.remove(query, RequestParam.class).getDeletedCount();
    }

    @Override
    public UserInfo getUserInfo(Long uid) {
        Query query = new Query(Criteria.where("_id").is(uid));
        return mongoTemplate.findOne(query, UserInfo.class, MongoConstant.WEIBO_USER_COLLECTION);
    }

    @Override
    public List<UserInfo> getInfoList(long page, int num) {
        Query query = new Query().limit(num).skip(page * num);
        List<UserInfo> userInfos = mongoTemplate.find(query, UserInfo.class, MongoConstant.WEIBO_USER_COLLECTION);
        return userInfos;
    }

    @Override
    public long userInfoCount() {
        return mongoTemplate.count(new Query(), MongoConstant.WEIBO_USER_COLLECTION);
    }
}
